# 测试登录界面
import time
from selenium.webdriver.common.by import By
from common.Utils import BlogDriver


class BlogLogin:
    url = ""
    driver = ""

    def __init__(self):
        url = ""
        driver = ""

        self.url = "http://8.137.19.140:9090/blog_login.html"
        self.driver = BlogDriver.driver
        self.driver.get(self.url)
        # BlogDriver.driver.get(self.url)

    # 成功登录的测试用例
    def LoginSucTest(self):
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        # 清除框中的内容
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()

        # 测试输入正确的 '账号'和'密码'能不能登录
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys('zhangsan')
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys('123456')
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

        # 如果成功登录的话能够找到博客首页用户昵称,否则登录失败
        self.driver.find_element(By.CSS_SELECTOR, 'body > div.container > div.left > div > h3')
        # 添加屏幕截图
        time.sleep(2)
        BlogDriver.ScreenShot()
        # # 在日志中体现
        BlogDriver.log("[debug] 账号和密码正确，成功登录")
        # 返回到登录页面
        self.driver.back()

    # 异常登录的测试用例
    def LoginFailTest(self):
        # 若需要连续多次测试，需要情况输入框中的内容
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()

        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhang")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        time.sleep(2)

        # 会发生弹窗，需要关闭不影响后续测试
        alter = self.driver.switch_to.alert
        alter.accept()
        # 记录日志
        BlogDriver.log("[debug] 错误账号或密码，登录失败")
        # 能够找到博客首页的昵称，证明登录成功，否则登录失败
        time.sleep(2)
        BlogDriver.ScreenShot()
        self.driver.back()
        # self.driver.quit()


# login = BlogLogin()
# login.LoginSucTest()
# login.LoginFailTest()
